package com.teemor.blog.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Pointcut("execution(public * com.teemor.blog.api.*.*(..))")
//    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void apiLog(){}

//    @Before("apiLog()")
    public void deBefor(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        StringBuilder sb = new StringBuilder();
        sb.append("\n").append("--------------------Api Request Log Start------------------").append("\n");
        sb.append("URL : ").append(request.getRequestURL().toString()).append("\n");
        sb.append("HTTP_METHOD : ").append(request.getMethod()).append("\n");
        sb.append("IP : ").append(request.getRemoteAddr()).append("\n");
        sb.append("CLASS_METHOD : ").append(joinPoint.getSignature().getDeclaringTypeName()).append(".").append(joinPoint.getSignature().getName()).append("\n");
        sb.append("ARGS : ").append(Arrays.toString(joinPoint.getArgs())).append("\n");
        sb.append("--------------------Api Request Log End------------------").append("\n");
        log.debug(sb.toString());
    }


    @Around("apiLog()")
    public Object deAfter(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("--------------------Api Response Log Start------------------").append("\n");
        sb.append("URL : ").append(request.getRequestURL().toString()).append("\n");
        sb.append("HTTP_METHOD : ").append(request.getMethod()).append("\n");
        sb.append("IP : ").append(request.getRemoteAddr()).append("\n");
        sb.append("CLASS_METHOD : ").append(joinPoint.getSignature().getDeclaringTypeName()).append(".").append(joinPoint.getSignature().getName()).append("\n");
        sb.append("ARGS : ").append(Arrays.toString(joinPoint.getArgs())).append("\n");
        Object object = null;
        try {
            object = joinPoint.proceed();
            sb.append("RESULTS : ").append(object).append("\n");
        } catch (Throwable throwable) {
            log.error("running exception",throwable);
            sb.append("--------------------Running Error------------------").append("\n");
            sb.append("--------------------Api Response Log End------------------").append("\n");
            log.error(sb.toString(), throwable);
            throw throwable;
        }
        sb.append("--------------------Api Response Log End------------------").append("\n");
        log.debug(sb.toString());
        return object;
    }
//    @AfterReturning(value = "apiLog()",returning = "value")
//    public void doAfterReturning(JoinPoint joinPoint,Object value){
//        sb.append("RETURN : "+value);
//    }

}
